package com.swu.pad.comment.dao;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;

/**
 * 因为Etherpad后台评论提取用SpringMVC读取不到连接，只能使用servlet
 * @author yuge
 *
 */
public class ConnectDatabase {

	private static String URL = "jdbc:mysql://localhost:3306/pad";
	private static String USER = "root";
	private static String PASSWORD = "123456";

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("加载Mysql数据库驱动失败！");
		}
	}

	private static Connection conn = null;

	/**
	 * 获取Connection
	 * 
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static Connection getConnection() throws SQLException, IOException {
		if (conn == null || conn.isClosed()) {
			try {

				conn = (Connection) DriverManager.getConnection(URL, USER, PASSWORD);
				// 获取数据库连接成功
				System.out.println("连接成功！");
			} catch (SQLException e) {
				System.out.println("获取数据库连接失败！");
				throw e;
			}
		}
		return conn;
	}

}
